package com.example.widgetcase.slice.taperchart;

import com.example.widget_lib.callback.ScrollObserver;
import com.example.widget_lib.utils.LogUtil;
import com.example.widget_lib.utils.PxUtil;
import com.example.widget_lib.widget.taperchart.TaperChart;
import com.example.widget_lib.widget.taperchart.TaperChartLayout;
import com.example.widgetcase.ResourceTable;
import com.example.widgetcase.slice.BaseSlice;
import com.example.widgetcase.slice.subject.ScrollSubscription;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.agp.utils.Color;

import java.security.SecureRandom;
import java.util.*;

public class TaperChartAbility extends BaseSlice {
    ScrollView taper_chart_scroll;
    TaperChart tchartV;
    TaperChart tchartV1;
    TaperChart tchartV2;
    TaperChart tchartV3;
    TaperChartLayout tchartLayout;
    TaperChartLayout tchartLayout1;
    TaperChartLayout tchartLayout2;
    Button contrastBtn;
    private ScrollSubscription subscription;

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_taper_chart;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        subscription = new ScrollSubscription();
        initView();
        syncTaperChartData(1);
        syncTaperChartData(2);
        syncTaperChartData(3);
        syncTaperChartData(4);
        syncTaperChartLayoutData();
        syncTaperChartLayoutDataDwColor();
        syncTaperChartLayoutDataDwColorCompar();
    }

    private void initView() {
        taper_chart_scroll = (ScrollView) findComponentById(ResourceTable.Id_taper_chart_scroll);
        taper_chart_scroll.setScrolledListener(new Component.ScrolledListener() {
            @Override
            public void onContentScrolled(Component component, int x, int y, int oldX, int oldY) {
                LogUtil.loge("scroll=> x: " + x + " y: " + y);
                subscription.notify(x, y);
            }
        });
        tchartV = (TaperChart) findComponentById(ResourceTable.Id_tchartV);
        //typeValue属性暂不可用，使用代码设置
        tchartV.setDrawTopValue(true);//是否绘制峰值顶尖值
        tchartV.setTextSize((int) PxUtil.fp2px(10f));//文字大小
        tchartV.setInnerSpace((int) PxUtil.vp2px(24f));// 图形相交长度
        subscription.attach(tchartV);

        tchartV1 = (TaperChart) findComponentById(ResourceTable.Id_tchartV1);
        tchartV1.setDrawTopValue(true);
        tchartV1.setTcMode(3);
        tchartV1.setInnerSpace((int) PxUtil.vp2px(28f));
        subscription.attach(tchartV1);

        tchartV2 = (TaperChart) findComponentById(ResourceTable.Id_tchartV2);
        tchartV2.setYAxisIsPercent(true);
        tchartV2.setTcMode(3);
        tchartV2.setInnerSpace((int) PxUtil.vp2px(28f));
        subscription.attach(tchartV2);

        tchartV3 = (TaperChart) findComponentById(ResourceTable.Id_tchartV3);
        tchartV3.setTcMode(4);
        tchartV3.setInnerSpace((int) PxUtil.vp2px(28f));
        subscription.attach(tchartV3);

        tchartLayout = (TaperChartLayout) findComponentById(ResourceTable.Id_tchartLayout);
        subscription.attach(tchartLayout);
        tchartLayout1 = (TaperChartLayout) findComponentById(ResourceTable.Id_tchartLayout1);
        subscription.attach(tchartLayout1);
        tchartLayout2 = (TaperChartLayout) findComponentById(ResourceTable.Id_tchartLayout2);
        subscription.attach(tchartLayout2);
        contrastBtn = (Button) findComponentById(ResourceTable.Id_contrastBtn);
        contrastBtn.setClickedListener((component -> {
            present(new ContrastAbility(), new Intent().setParam("name", "对比峰值图"));
        }));
    }

    private void syncTaperChartData(int type) {
        int green = Color.rgb(0, 255, 0);
        int gray = Color.rgb(128, 128, 128);
        int cyan = Color.rgb(0, 255, 255);
        int red = Color.rgb(255, 0, 0);
        switch (type) {
            case 1: {
                tchartV.isShowDebugView(false);//是否开启调试模式
                tchartV.setTaperColors(green, gray, red);

                List<String> keys = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    keys.add("第" + (i + 1) + "次");
                }
                List<Float> values = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    int randomInt = new SecureRandom().nextInt(100);
                    values.add((float) ((i + 1) * randomInt));
                }
                tchartV.setData(keys, values);
                break;
            }
            case 2: {
                tchartV1.offSetXy(96f);
                tchartV1.isShowDebugView(true);//是否开启调试模式
                tchartV1.setHintIsRatio(true);
                List<String> keys = new ArrayList<>();
                for (int i = 0; i < 4; i++) {
                    keys.add("第" + (i + 1) + "次");
                }
                List<Float> values = new ArrayList<>();
                for (int i = 0; i < 4; i++) {
                    int randomInt = new SecureRandom().nextInt(100);
                    values.add((float) ((i + 1) * randomInt));
                }
                String[] labels = new String[]{"男性", "女性"};
                tchartV1.setData(keys, values, labels);

                break;
            }
            case 3: {
                tchartV2.offSetXy(48f);
                tchartV2.isShowDebugView(true);
                List<String> keys = new ArrayList<>();
                for (int i = 0; i < 4; i++) {
                    keys.add("第" + (i + 1) + "次");
                }
                List<Float> values = new ArrayList<>();
                for (int i = 0; i < 4; i++) {
                    double randomFloat = new SecureRandom().nextFloat();
                    values.add((float)randomFloat);
                }
                String[] labels = new String[]{"男性", "女性"};
                tchartV2.setData(keys, values, labels);
                break;
            }
            case 4: {
                tchartV3.offSetXy(48f);
                tchartV3.isShowDebugView(true);
                tchartV3.setTaperColors(green, cyan);

                List<String> keys = new ArrayList<>();
                for (int i = 0; i < 6; i++) {
                    keys.add("第" + (i + 1) + "次");
                }
                List<Float> values = new ArrayList<>();
                for (int i = 0; i < 6; i++) {
                    int randomInt = new SecureRandom().nextInt(100);
                    values.add((float) randomInt);
                }
                String[] labels = new String[]{"30min以内", "30-45min", "45min以上"};
                tchartV3.setData(keys, values, labels);
                break;
            }
        }
    }


    private void syncTaperChartLayoutData() {
        tchartLayout.getChart().setDrawTopValue(true);
        List<String> keys = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            keys.add("第" + (i + 1) + "次");
        }
        List<String> labels = new ArrayList<>();
        labels.add("( <30分 )");
        labels.add("( 30分-45分 )");
        labels.add("( >45分 )");

        List<Float> values = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            int randomInt = new SecureRandom().nextInt(100);
            values.add((float) ((i + 1) * randomInt));
        }
        tchartLayout.setData(keys, values, labels);
    }

    private void syncTaperChartLayoutDataDwColor() {
        int green = Color.rgb(0, 255, 0);
        int lightGray = Color.rgb(211, 211, 211);
        int red = Color.rgb(255, 0, 0);
        tchartLayout1.setColors(green, lightGray, red);

        List<String> keys = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            keys.add("第" + (i + 1) + "次");
        }
        List<String> labels = new ArrayList<>();
        labels.add("( <30分 )");
        labels.add("( 30分-45分 )");
        labels.add("( >45分 )");

        List<Float> values = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            int randomInt = new SecureRandom().nextInt(100);
            values.add((float) ((i + 1) * randomInt));
        }

        String[] btmLabels = new String[]{"第一次", "第二次", "第三次"};
        tchartLayout1.setData(keys, values, btmLabels, "次", labels, false);
    }

    private void syncTaperChartLayoutDataDwColorCompar() {
        int green = Color.rgb(0, 255, 0);
        int blue = Color.rgb(0, 0, 255);

        tchartLayout2.getChart().setTcMode(4);
        tchartLayout2.getChart().offSetXy(48f);
        tchartLayout2.getChart().isShowDebugView(false);
        tchartLayout2.getChart().setDrawTopValue(true);
        tchartLayout2.setColors(green, blue);

        List<String> keys = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            keys.add("第" + (i + 1) + "次");
        }

        List<Float> values = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            int randomInt = new SecureRandom().nextInt(100);
            values.add((float) ((i + 1) * randomInt));
        }

        String[] labels = {"30min以内", "30-45min", "45min以上"};

        List<String> btmLabels = new ArrayList<>();
        btmLabels.add("( <30分 )");
        btmLabels.add("( 30分-45分 )");
        btmLabels.add("( >45分 )");

        tchartLayout2.setData(keys, values, labels, "次", btmLabels, true);
    }

}
